<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="配置名称" prop="name" style="width:400px">
      <el-input v-model="dataForm.name" placeholder="配置名称"></el-input>
    </el-form-item>
    <el-form-item label="角标类型" prop="cornerType">
      <el-select v-model="dataForm.cornerType">
        <el-option
          v-for="item in cornerTypeOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="角标样式" prop="imgUrl">
      <div class="bannerImg-wrapper pic">
          <el-upload
            ref="uploadBannerImg"
            :action="uploadBannerImgUrl"
            :show-file-list="false"
            :on-success="handleBannerImgSuccess"
            :on-error="handleBannerImgError"
            :before-upload="beforeBannerImgUpload">
            <img v-if="dataForm.imgUrl" :src="dataForm.imgUrl" class="bannerImg">
            <i v-else class="el-icon-plus bannerImg-uploader-icon"></i>
          </el-upload>
        </div>
    </el-form-item>
    <el-form-item label="生效内容" prop="brandCode" v-if="dataForm.cornerType=='brand'">
      <el-select v-model="dataForm.brandCode">
        <el-option
          v-for="item in sourceOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <!-- <el-form-item label="当角标为品牌时,品牌code(迪士尼,上海电影等)" prop="brandCode">
      <el-input v-model="dataForm.brandCode" placeholder="当角标为品牌时,品牌code(迪士尼,上海电影等)"></el-input>
    </el-form-item>
    <el-form-item label="角标位置(p2:右下角,p3:右上角,p4:左上角)" prop="position">
      <el-input v-model="dataForm.position" placeholder="角标位置(p2:右下角,p3:右上角,p4:左上角)"></el-input>
    </el-form-item> -->
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注" type="textarea" rows="2"></el-input>
    </el-form-item>
    <!-- <el-form-item label="状态(1上架,0下架)" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态(1上架,0下架)"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          name: '',
          cornerType: '',
          imgUrl: '',
          brandCode: '',
          position: '',
          remark: '',
          status: ''
        },
        cornerTypeOptions: [],
        sourceOptions: [],
        uploadBannerImgUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        dataRule: {
          name: [
            { required: true, message: '配置名称不能为空', trigger: 'blur' }
          ],
          cornerType: [
            { required: true, message: '角标类型(hot/new/推荐/收费/vip/限免/品牌等)不能为空', trigger: 'blur' }
          ],
          imgUrl: [
            { required: true, message: '角标图片不能为空', trigger: 'blur' }
          ],
          brandCode: [
            { required: true, message: '生效内容不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '角标位置不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1上架,0下架)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getCornerType()
      this.getSource()
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        console.log(id)
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbcorner/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.corner
                // this.dataForm.name = data.pbcorner.name
                // this.dataForm.cornerType = data.pbcorner.cornerType
                // this.dataForm.imgUrl = data.pbcorner.imgUrl
                // this.dataForm.brandCode = data.pbcorner.brandCode
                // this.dataForm.position = data.pbcorner.position
                // this.dataForm.remark = data.pbcorner.remark
                // this.dataForm.status = data.pbcorner.status
              }
            })
          } else {
            this.dataForm.name = ''
            this.dataForm.cornerType = '',
            this.dataForm.imgUrl = '',
            this.dataForm.brandCode = '',
            this.dataForm.position = '',
            this.dataForm.remark = '',
            this.dataForm.status = ''
          }
        })
      },
      // 获取角标类型
      getCornerType () {
        this.$http({
              url: this.$http.adornUrl(`/picbook/combobox/listCornerType`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.cornerTypeOptions = data.data
              }
            })
      },
      // 获取角标来源
      getSource () {
        this.$http({
              url: this.$http.adornUrl(`/picbook/combobox/listSource`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.sourceOptions = data.data
              }
            })
      },
      handleBannerImgSuccess (res, file) {
        this.dataForm.imgUrl = res.url
      },
      beforeBannerImgUpload (file) {
        if (file.type.indexOf('image') < 0) {
          this.$message.error('图片格式不正确')
          return false
        }
      },
      handleBannerImgError (err, file, fileList) {
        this.$message.error('上传失败')
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/pbcorner/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                corner: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  .bannerImg-wrapper {
    .bannerImg-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      border: 1px solid #d9d9d9;
      border-radius: 6px;
      width: 120px;
      height: 120px;
      line-height: 120px;
      text-align: center;
    }
    .bannerImg {
      width: 120px;
      height: 120px;
      display: block;
    }
  }
</style>